package com.project.movie.controller.price;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.movie.common.enums.BusinessType;
import com.project.movie.common.enums.ResultCode;
import com.project.movie.config.utils.ShiroUtils;
import com.project.movie.domain.*;
import com.project.movie.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 排片价格controller
 * @date 2025/02/07 11:31
 */
@Controller
@ResponseBody
@RequestMapping("price")
public class MoviePriceController {

    @Autowired
    private MoviePriceService moviePriceService;
    @Autowired
    private MovieSeatService movieSeatService;
    @Autowired
    private MovieCinemaService movieCinemaService;
    @Autowired
    private MovieItemService movieItemService;
    @Autowired
    private MovieOfficeService movieOfficeService;
    @Autowired
    private MovieSliceService movieSliceService;

    /** 分页获取排片价格 */
    @PostMapping("getMoviePricePage")
    public Result getMoviePricePage(@RequestBody MoviePrice moviePrice) {
        Page<MoviePrice> page = new Page<>(moviePrice.getPageNumber(),moviePrice.getPageSize());
        QueryWrapper<MoviePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(moviePrice.getItemId()),MoviePrice::getItemId,moviePrice.getItemId())
                .eq(StringUtils.isNotBlank(moviePrice.getOfficeId()),MoviePrice::getOfficeId,moviePrice.getOfficeId())
                .eq(StringUtils.isNotBlank(moviePrice.getSliceId()),MoviePrice::getSliceId,moviePrice.getSliceId())
                .eq(moviePrice.getRowIndex() != null,MoviePrice::getRowIndex,moviePrice.getRowIndex())
                .eq(moviePrice.getColumnIndex() != null,MoviePrice::getColumnIndex,moviePrice.getColumnIndex())
                .eq(moviePrice.getPrice() != null,MoviePrice::getPrice,moviePrice.getPrice())
                .eq(moviePrice.getState() != null,MoviePrice::getState,moviePrice.getState());
        Page<MoviePrice> moviePricePage = moviePriceService.page(page, queryWrapper);
        return Result.success(moviePricePage);
    }

    @PostMapping("getMoviePriceList")
    public Result getMoviePriceList(@RequestBody MoviePrice moviePrice) {
        QueryWrapper<MoviePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(moviePrice.getItemId()),MoviePrice::getItemId,moviePrice.getItemId())
                .eq(StringUtils.isNotBlank(moviePrice.getOfficeId()),MoviePrice::getOfficeId,moviePrice.getOfficeId())
                .eq(StringUtils.isNotBlank(moviePrice.getSliceId()),MoviePrice::getSliceId,moviePrice.getSliceId())
                .eq(moviePrice.getRowIndex() != null,MoviePrice::getRowIndex,moviePrice.getRowIndex())
                .eq(moviePrice.getColumnIndex() != null,MoviePrice::getColumnIndex,moviePrice.getColumnIndex())
                .eq(moviePrice.getPrice() != null,MoviePrice::getPrice,moviePrice.getPrice())
                .eq(moviePrice.getState() != null,MoviePrice::getState,moviePrice.getState());
        List<MoviePrice> moviePricePage = moviePriceService.list(queryWrapper);
        return Result.success(moviePricePage);
    }

    @PostMapping("getMoviePriceBuy")
    public Result getMoviePriceBuy(@RequestBody MoviePrice moviePrice) {
        QueryWrapper<MoviePrice> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MoviePrice::getSliceId,moviePrice.getSliceId())
                .groupBy(MoviePrice::getRowIndex).orderByAsc(MoviePrice::getRowIndex);
        //获取行分组
        List<MoviePrice> priceList = moviePriceService.list(queryWrapper);
        //获取列分组
        for (MoviePrice price : priceList) {
            QueryWrapper<MoviePrice> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(MoviePrice::getSliceId,moviePrice.getSliceId())
                    .eq(MoviePrice::getRowIndex,price.getRowIndex()).orderByAsc(MoviePrice::getColumnIndex);
            List<MoviePrice> prices = moviePriceService.list(wrapper);
            price.setMovieItems(prices);
        }
        return Result.success(priceList);
    }

    /** 根据id获取排片价格 */
    @GetMapping("getMoviePriceById")
    public Result getMoviePriceById(@RequestParam("id")String id) {
        MoviePrice moviePrice = moviePriceService.getById(id);
        return Result.success(moviePrice);
    }

    @GetMapping("getMoviePriceByIds")
    public Result getMoviePriceByIds(@RequestParam("ids")String ids) {
        String[] split = ids.split(",");
        List<MoviePrice> list = new ArrayList<>();
        int money = 0;
        List<String> index = new ArrayList<>();
        for (String id:split) {
            MoviePrice moviePrice = moviePriceService.getById(id);
            money += moviePrice.getPrice();
            int row = moviePrice.getRowIndex() + 1;
            int column = moviePrice.getColumnIndex() + 1;
            index.add(row + "排" + column + "座");
            list.add(moviePrice);
        }
        //设置订单参数
        String id = split[0];
        MoviePrice price = moviePriceService.getById(id);
        MovieItem movieItem = movieItemService.getById(price.getItemId());
        MovieOffice office = movieOfficeService.getById(price.getOfficeId());
        MovieSlice slice = movieSliceService.getById(price.getSliceId());
        MovieCinema cinema = movieCinemaService.getById(slice.getCinemaId());
        MovieOrder movieOrder = new MovieOrder();
        movieOrder.setItemId(movieItem.getId());
        movieOrder.setItemName(movieItem.getName());
        User user = ShiroUtils.getUserInfo();
        movieOrder.setUserId(user.getId());
        movieOrder.setPriceId(ids);
        movieOrder.setCinemaId(cinema.getId());
        movieOrder.setCinemaName(cinema.getName());
        movieOrder.setOfficeId(office.getId());
        movieOrder.setOfficeName(office.getName());
        movieOrder.setPrice(money);
        movieOrder.setSliceId(slice.getId());
        movieOrder.setPriceName(StringUtils.join(index,","));
        movieOrder.setLookDate(slice.getLookDate());
        movieOrder.setStart(slice.getStart());
        movieOrder.setTel(user.getTel());
        movieOrder.setEmail(user.getEmail());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("priceList",list);
        jsonObject.put("order",movieOrder);
        return Result.success(jsonObject);
    }

    /** 保存排片价格 */
    @PostMapping("saveMoviePrice")
    public Result saveMoviePrice(@RequestBody MoviePrice moviePrice) {
        boolean save = false;
        if (StringUtils.isNotBlank(moviePrice.getId())) {
            save = moviePriceService.updateById(moviePrice);
        } else {
            save = moviePriceService.save(moviePrice);
        }
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    @PostMapping("saveMoviePriceAll")
    public Result saveMoviePriceAll(@RequestBody MoviePrice moviePrice) {
        //先去查座位
        QueryWrapper<MovieSeat> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(MovieSeat::getOfficeId,moviePrice.getOfficeId());
        List<MovieSeat> seatList = movieSeatService.list(wrapper);
        for (int i = 0; i < seatList.size(); i++) {
            MovieSeat movieSeat = seatList.get(i);
            for (int j = 0; j < movieSeat.getNum(); j++) {
                QueryWrapper<MoviePrice> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(MoviePrice::getItemId,moviePrice.getItemId())
                        .eq(MoviePrice::getOfficeId,moviePrice.getOfficeId())
                        .eq(MoviePrice::getSliceId,moviePrice.getSliceId())
                        .eq(MoviePrice::getRowIndex,i)
                        .eq(MoviePrice::getColumnIndex,j)
                        .last("limit 1");
                MoviePrice price = moviePriceService.getOne(queryWrapper);
                if (price != null) {
                    if (price.getState() != 0) {
                        price.setPrice(moviePrice.getPrice());
                        moviePriceService.updateById(price);
                    }
                } else {
                    MoviePrice moviePrice1 = new MoviePrice();
                    moviePrice1.setPrice(moviePrice.getPrice());
                    moviePrice1.setItemId(moviePrice.getItemId());
                    moviePrice1.setOfficeId(moviePrice.getOfficeId());
                    moviePrice1.setSliceId(moviePrice.getSliceId());
                    moviePrice1.setRowIndex(i);
                    moviePrice1.setColumnIndex(j);
                    moviePriceService.save(moviePrice1);
                }
            }
        }
        return Result.success();
    }

    /** 编辑排片价格 */
    @PostMapping("editMoviePrice")
    public Result editMoviePrice(@RequestBody MoviePrice moviePrice) {
        boolean save = moviePriceService.updateById(moviePrice);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除排片价格 */
    @GetMapping("removeMoviePrice")
    public Result removeMoviePrice(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                moviePriceService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("排片价格id不能为空！");
        }
    }

}
